from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from config import config

admin_email = config.admin_email()

def send_email(receiver,ramdom_number):
  """
  :param receiver: 收件人的email
  :param ramdom_number: 生成的随机验证码
  :return:
  """
  #qq邮箱smtp服务器
  host_server = admin_email['email_type']
  #pwd为qq邮箱的授权码
  pwd = admin_email['email_password']
  #发件人的邮箱
  sender_mail = admin_email['email_address']
  #收件人邮箱
  receiver = receiver

  #邮件的正文内容
  mail_content = """
      <h2 style="text-align: center">DomainTrade验证</h2>
      <p style="text-align: center">随机验证码为: %s</p>
      <p style="text-align: center">此验证码在5分钟内有效</p>
      """ %(ramdom_number)
  #邮件标题
  mail_title = 'DomainTrade验证码'

  #ssl登录
  smtp = SMTP_SSL(host_server)
  #set_debuglevel()是用来调试的。参数值为1表示开启调试模式，参数值为0关闭调试模式
  # smtp.set_debuglevel(1)
  smtp.ehlo(host_server)
  smtp.login(sender_mail, pwd)

  msg = MIMEText(mail_content, "html", 'utf-8')
  msg["Subject"] = Header(mail_title, 'utf-8')
  msg["From"] = sender_mail
  msg["To"] = Header("You", 'utf-8') # 接收者的别名

  smtp.sendmail(sender_mail, receiver, msg.as_string())
  smtp.quit()


def send_reserve_info(user_name, user_tel, domain_name, price, now_time):
  """
  域名预定成功后,发送邮件通知管理员
  :param user_name:
  :param user_tel:
  :param domain_name:
  :param price:
  :return:
  """
  # 邮箱smtp服务器
  host_server = admin_email['email_type']
  # pwd为qq邮箱的授权码
  pwd = admin_email['email_password']
  # 发件人的邮箱
  sender_mail = admin_email['email_address']
  # 收件人邮箱
  receiver = admin_email['admin_email']

  # 邮件的正文内容
  mail_content = """
        {user_email}用户, 与{now_time}, 预定{domain_name}域名, 交易价格为{price},
        用户的邮箱为{user_email}, 电话号码为{tel}. 
        """ .format_map({'user_email': user_name,'now_time': now_time,
                         'domain_name':domain_name, 'price': price,
                         'tel': user_tel })
  # 邮件标题
  mail_title = 'DomainTrade验证码'

  # ssl登录
  smtp = SMTP_SSL(host_server)
  # set_debuglevel()是用来调试的。参数值为1表示开启调试模式，参数值为0关闭调试模式
  # smtp.set_debuglevel(1)
  smtp.ehlo(host_server)
  smtp.login(sender_mail, pwd)

  msg = MIMEText(mail_content, "html", 'utf-8')
  msg["Subject"] = Header(mail_title, 'utf-8')
  msg["From"] = sender_mail
  msg["To"] = Header("You", 'utf-8')  # 接收者的别名

  smtp.sendmail(sender_mail, receiver, msg.as_string())
  smtp.quit()